<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 15:22
 */

namespace app\home\controller;

use think\Db;

class Release extends Base
{

    /**
     * 发布房源
     * @return mixed|\think\response\Json
     */
    public function newHouse()
    {
        extract(input());
        if (request()->isPost()) {
            extract(input());
            $user_id = session('user_id');
            //1、先获取用户权限和用户信息
            $auth = Db::table('up_u_user')
                ->where('user_id',$user_id)
                ->value('user_auth');
            $config = Db::table('up_member_config')
                ->where('mc_auth',$auth)
                ->find();
            $user = Db::table('up_u_user')
                ->where('user_id', $user_id)
                ->field('user_type,user_status,user_auth,user_fabu,user_phone,user_all_fabu')
                ->find();
            $have_fabu = $config['mc_fabu'];
            //
            //1、如果我是经纪人没有认证
            if ($user['user_type'] == 2 && $user['user_status'] != 1) {
                return json(['status' => 0, 'msg' => '请先去实名认证']);
            }
            //2、超过了发布次数
            if ($user['user_fabu'] >= $have_fabu) {
                return json(['status' => 0, 'msg' => '超过了发布次数']);
            }
            //3、针对新人
            if($user['user_auth'] == 1 && $user['user_all_fabu'] >= $have_fabu){
                return json(['status'=>0,'msg'=>'您累计已经发布了' . $have_fabu . '次，请开通会员!']);
            }
            $data = array();
            if($h_images){
                $h_images = trim($h_images, ',');
                $imgarray = explode(',',$h_images);
                foreach ($imgarray as $k=>$v){
                    $imgarray[$k] = $v . '-333';
                }
                $h_images = implode($imgarray,',');
            }else{
                return json(['status' => 0, 'msg' => '图片不能为空']);
            }
            $data['h_user_id'] = $user_id;
            $data['h_user_type'] = $user['user_type'];
            $data['h_title'] = $h_title;
            $data['h_images'] = trim($h_images, ',');
            $data['h_img'] = explode(',', $data['h_images'])[0];
            if($h_video != ''){
                $data['h_video'] = trim($h_video, ',');
            }else{
                $data['h_video'] = '';
            }
            $data['h_xiaoqu'] = $h_xiaoqu;
            $data['h_mianji'] = $h_mianji;
            $data['h_price'] = $h_price;
            $data['h_unit_price'] = ($h_price/$h_mianji)*10000;
            $data['h_shis'] = $h_shis;
            $data['h_tings'] = $h_tings;
            $data['h_weis'] = $h_weis;
            $data['h_fix'] = $h_fix;
            $data['h_floor'] = $h_floor;
            if(isset($h_first)){
                $data['h_first'] = $h_first;
            }
            $data['h_sign'] = ',' . trim($h_sign, ',') . ',';
//        $data['h_remark'] = $h_remark;
            $data['h_jianjie'] = $h_jianjie;
            $data['h_contact'] = $user['user_phone'];
            $data['h_type'] = $h_type;
            $data['h_sheng'] = $h_sheng;
            $data['h_shi'] = $h_shi;
            $data['h_qu'] = $h_qu;
            $data['h_street'] = $h_street;
            $jw = $latlonsure;
            if($latlonsure == '116.407526,39.90403' && $latlon != ''){
                $jw = $latlon;
            }
            $jw_a = explode(',',$jw);
            $data['h_lat'] = $jw_a[1];
            $data['h_lon'] = $jw_a[0];
            $data['h_time'] = date('Y-m-d H:i:s');
            $data['h_listtime'] = date('Y-m-d H:i:s');
            $data['h_updatetime'] = date('Y-m-d H:i:s');
            $data['h_order'] = $user['user_type'];
            if(isset($h_shui)){
                $data['h_shui'] = $h_shui;
            }
            $data['h_kan'] = $h_kan;
            $data['h_face'] = $h_face;
            if(isset($h_more) && $h_more){
                $data['h_more'] = implode($h_more, ',');
            }else{
                $data['h_more'] = '';
            }
            $data['h_dis_id'] = $h_dis_id;
            if($h_dis_id && $h_dis_id > 0){
                $llll = Db::table('up_district')->where('dis_id', $h_dis_id)->field('dis_lat,dis_lon')->find();
                $data['h_lat'] = $llll['dis_lat'];
                $data['h_lon'] = $llll['dis_lon'];
            }
            $data['h_hot_title'] = trim($h_hot_title,' ');
            $res = Db::table('up_house')->insert($data);
            if ($res) {
                Db::table('up_u_user')->where('user_id', $user_id)->setInc('user_fabu');
                Db::table('up_u_user')->where('user_id', $user_id)->setInc('user_all_fabu');
                if ($user['user_type'] == 2) {
                    $apibase = new \app\api\controller\Base();
                    $apibase->updateHouseList($h_type, $h_shi);
                }
                return json(['status' => 200, 'msg' => '发布成功']);
            } else {
                return json(['status' => 0, 'msg' => '发布失败']);
            }
        }
        $user_type = Db::table('up_u_user')->where('user_id', session('user_id'))->value('user_type');
        if($user_type == 3){
            return $this->fetch('release/addcase');
        }else{
            //省
            $pros = Db::table('up_area')
                ->where('level', 1)
                ->field('district_id,district')
                ->select();
            //
            if(!session('?my_location_city_id')){
                $shi = 1002;
            }else{
                $shi = session('my_location_city_id');
            }
            $diss = Db::table('up_district')
                ->where('dis_shi',$shi)
                ->order('dis_update DESC')
                ->field('dis_id,dis_name')
                ->select();
            if(isset($type) && $type){
                if($type == 2){
                    return $this->fetch('release/issue_er', ['pros' => $pros , 'diss' => $diss]);
                }else{
                    return $this->fetch('release/issue_xin', ['pros' => $pros , 'diss' => $diss]);
                }
            }else{
                return $this->fetch('release/issue', ['pros' => $pros , 'diss' => $diss]);
            }
        }

    }
    /**
     * 发布案例
     * @return mixed|\think\response\Json
     */
    public function newCase()
    {
        if (request()->isPost()) {
            extract(input());
            $user_id = session('user_id');
            $user = Db::table('up_u_user')
                ->where('user_id', $user_id)
                ->field('user_type,user_status,user_company_id_3')
                ->find();
            //1、如果我没有认证
            if ($user['user_status'] != 1) {
                return json(['status' => 0, 'msg' => '请先去实名认证']);
            }
            $data = array();
            if($h_images){
                $h_images = trim($h_images, ',');
                $imgarray = explode(',',$h_images);
                foreach ($imgarray as $k=>$v){
                    $imgarray[$k] = $v . '-333';
                }
                $h_images = implode($imgarray,',');
            }else{
                return json(['status' => 0, 'msg' => '图片不能为空']);
            }
            $data['cc_c_id'] = $user['user_company_id_3'];
            $data['cc_u_id'] = $user_id;
            $data['cc_title'] = $h_title;
            $data['cc_name'] = $h_hot_title;
            $data['cc_image'] = trim($h_images, ',');
            $data['cc_thumb'] = explode(',', $data['cc_image'])[0];
            $data['cc_style'] = $h_xiaoqu;
            $data['cc_content'] = $h_jianjie;
            $res = Db::table('up_company_case')->insert($data);
            if ($res) {
                Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_cases_3');
                return json(['status' => 200, 'msg' => '发布成功']);
            } else {
                return json(['status' => 0, 'msg' => '发布失败']);
            }
        }else{
            echo 'error！！！';
        }
    }

    public function forTaskFabuCheck($task_type = '', $task_sheng = '', $task_shi = '', $task_qu = '', $task_num = '', $task_price = '', $task_name = '', $task_starttime = '', $task_endtime = '', $task_content = '', $task_href = '', $task_href_ios = '', $task_app_ios = '', $task_app_android = '')
    {
        $res = [
            'status' => 200,
            'msg' => 'OK'
        ];
        if (empty($task_type)) {
            $res['status'] = 0;
            $res['msg'] = '任务类型不能为空';
            return $res;
        }
        if ($task_type != 5) {
            if (empty($task_href)) {
                $res['status'] = 0;
                $res['msg'] = '任务链接不能为空';
                return $res;
            }
        }
        if ($task_type == 1) {
            if (!$task_href_ios || !$task_app_ios || !$task_app_android) {
                $res['status'] = 0;
                $res['msg'] = '任务各下载链接不能为空';
                return $res;
            }
        }
        if (empty($task_sheng)) {//试试-1是不是为空
            $res['status'] = 0;
            $res['msg'] = '请选择任务区域';
            return $res;
        } else {
            if ($task_sheng != -1) {
                if (empty($task_shi)) {
                    $res['status'] = 0;
                    $res['msg'] = '请选择任务所在市';
                    return $res;
                } else {
                    if ($task_shi != -1) {
                        if (empty($task_qu)) {
                            $res['status'] = 0;
                            $res['msg'] = '请选择任务所在区';
                            return $res;
                        }
                    }

                }
            }
        }
        if (empty($task_num)) {
            $res['status'] = 0;
            $res['msg'] = '任务总数不能为空';
            return $res;
        }
        if (empty($task_price)) {
            $res['status'] = 0;
            $res['msg'] = '任务单个金额不能为空';
            return $res;
        }
        if (empty($task_name)) {
            $res['status'] = 0;
            $res['msg'] = '任务名称不能为空';
            return $res;
        }
        if (empty($task_starttime) || empty($task_endtime)) {
            $res['status'] = 0;
            $res['msg'] = '任务开始时间或结束时间不能为空';
            return $res;
        } else {
            $today = date('Y-m-d H:i:s');
            if ($task_starttime <= $today) {
                $res['status'] = 0;
                $res['msg'] = '任务开始时间不能小于当前时间';
                return $res;
            }
            $cb = (strtotime($task_endtime) - strtotime($task_starttime)) / 3600;
            if ($cb < 48) {
                $res['status'] = 0;
                $res['msg'] = '任务开始时间不能大于结束时间且相隔不能低于48小时';
                return $res;
            }
        }
        if ($task_content == '<p><br></p>' || $task_content == '<p></p><p><br></p>') {
            $res['status'] = 0;
            $res['msg'] = '任务简介不能为空';
            return $res;
        }
        return $res;
    }


    /**
     * 获得省市区对应数据
     * @return \think\response\Json
     */
    public function getAreaByType()
    {
        $type = $_POST['type'];
        $id = $_POST['id'];
        $shi = Db::table('up_area')
            ->where('level', $type)
            ->where('pid', $id)
            ->field('district_id,district')
            ->select();
        return json(['status' => 200, 'msg' => $shi]);

    }
}